package com.ruoyi.blogs.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.blogs.mapper.BsBrowseHistoryMapper;
import com.ruoyi.blogs.mapper.BsFaqMapper;
import com.ruoyi.blogs.mapper.BsNoteMapper;
import com.ruoyi.blogs.mapper.BsResourceMapper;
import com.ruoyi.blogs.domain.BsBrowseHistory;
import com.ruoyi.blogs.domain.BsFaq;
import com.ruoyi.blogs.domain.BsNote;
import com.ruoyi.blogs.domain.BsResource;
import com.ruoyi.blogs.service.IBsBrowseHistoryService;

/**
 * 浏览历史Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-06
 */
@Service
public class BsBrowseHistoryServiceImpl implements IBsBrowseHistoryService 
{
    @Autowired
    private BsBrowseHistoryMapper bsBrowseHistoryMapper;

    @Autowired
    private BsNoteMapper bsNoteMapper;

    @Autowired
    private BsFaqMapper bsFaqMapper;

    @Autowired
    private BsResourceMapper bsResourceMapper;

    /**
     * 查询浏览历史
     * 
     * @param id 浏览历史主键
     * @return 浏览历史
     */
    @Override
    public BsBrowseHistory selectBsBrowseHistoryById(String id)
    {
        return bsBrowseHistoryMapper.selectBsBrowseHistoryById(id);
    }

    /**
     * 查询浏览历史列表
     * 
     * @param bsBrowseHistory 浏览历史
     * @return 浏览历史
     */
    @Override
    public List<BsBrowseHistory> selectBsBrowseHistoryList(BsBrowseHistory bsBrowseHistory)
    {
        return bsBrowseHistoryMapper.selectBsBrowseHistoryList(bsBrowseHistory);
    }

    /**
     * 新增浏览历史
     * 
     * @param bsBrowseHistory 浏览历史
     * @return 结果
     */
    @Override
    public int insertBsBrowseHistory(BsBrowseHistory bsBrowseHistory)
    {
        // 查询是否已添加改历史记录
        bsBrowseHistory.setUserId(SecurityUtils.getUserId());
        List<BsBrowseHistory> list = bsBrowseHistoryMapper.selectBsBrowseHistoryList(bsBrowseHistory);
        if(list.size() > 0){
            // 重复更新浏览时间
            BsBrowseHistory history = list.get(0);
            history.setCreateTime(DateUtils.getNowDate());
            return bsBrowseHistoryMapper.updateBsBrowseHistory(history);
        }
        bsBrowseHistory.setId(IdUtils.fastSimpleUUID());
        bsBrowseHistory.setCreateTime(DateUtils.getNowDate());
        // 根据类型查询连接id并设置
        if(bsBrowseHistory.getType() == 0){
            BsNote bsNote = bsNoteMapper.selectBsNoteById(bsBrowseHistory.getConnectId());
            bsBrowseHistory.setConnectUserId(bsNote.getAuthorId());
        } else if(bsBrowseHistory.getType() == 1){
            BsFaq bsFaq = bsFaqMapper.selectBsFaqById(bsBrowseHistory.getConnectId());
            bsBrowseHistory.setConnectUserId(bsFaq.getUserId());
        } else {
            BsResource bsResource = bsResourceMapper.selectBsResourceById(bsBrowseHistory.getConnectId());
            bsBrowseHistory.setConnectUserId(bsResource.getUserId());
        }
        return bsBrowseHistoryMapper.insertBsBrowseHistory(bsBrowseHistory);
    }

    /**
     * 修改浏览历史
     * 
     * @param bsBrowseHistory 浏览历史
     * @return 结果
     */
    @Override
    public int updateBsBrowseHistory(BsBrowseHistory bsBrowseHistory)
    {
        return bsBrowseHistoryMapper.updateBsBrowseHistory(bsBrowseHistory);
    }

    /**
     * 批量删除浏览历史
     * 
     * @param ids 需要删除的浏览历史主键
     * @return 结果
     */
    @Override
    public int deleteBsBrowseHistoryByIds(String[] ids)
    {
        return bsBrowseHistoryMapper.deleteBsBrowseHistoryByIds(ids);
    }

    /**
     * 删除浏览历史信息
     * 
     * @param id 浏览历史主键
     * @return 结果
     */
    @Override
    public int deleteBsBrowseHistoryById(String id)
    {
        return bsBrowseHistoryMapper.deleteBsBrowseHistoryById(id);
    }

    @Override
    public int deleteBsBrowseHistorySevenDay() {
        return bsBrowseHistoryMapper.deleteBsBrowseHistorySevenDay();
    }
}
